package com.qifenqian.turbot.config

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.net.ConnectivityManager
import com.qifenqian.turbot.util.NetUtils
import org.jetbrains.anko.toast

class NetReceiver : BroadcastReceiver() {
    private var mNetConnected: Boolean = false
    fun isNetConnected(): Boolean {
        return mNetConnected
    }

    override fun onReceive(context: Context, intent: Intent) {
        mNetConnected = if (!NetUtils.isNetworkAvailable(context)) {
            context.toast("没有网络")
            false
        } else {
            true
        }
        val action = intent.action
        if (ConnectivityManager.CONNECTIVITY_ACTION == action) {
            val connectivityManager = TurbotApplication.instance().getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
            val info = connectivityManager.activeNetworkInfo
            if (info != null && info.isAvailable()) {
//                WsManager.getInstance().reconnect();//wify 4g切换重连websocket
            }
        }
    }
}
